package com.tanvir.sdl.client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.File;
import java.io.InputStream;

import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class DLFrame
    extends JFrame {
    private static Logger logger = Logger.getLogger(DLFrame.class.getPackage().getName());
    private BorderLayout layoutMain = new BorderLayout();
    private JPanel panelCenter = new JPanel();
    private JLabel statusBar = new JLabel();
    private ImageIcon imageOpen = new ImageIcon(DLFrame.class.getResource("openfile.gif"));
    private JFileChooser jFile1 = new JFileChooser(new File("."));
    private JButton jButton1 = new JButton();

    private MainThread mt;

    private static String MOVIES_URL = "http://www.indiafms.com/music/category/view/347/movies";
    private JLabel jLabel1 = new JLabel();
    private JButton jButton2 = new JButton();
    private JButton jButton3 = new JButton();

    public DLFrame() {
        try {
            LogManager lm = LogManager.getLogManager();
            InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties");
            if (stream != null) {
                lm.readConfiguration(stream);
            } else {
                logger.setLevel(Level.INFO);
            }
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit()
        throws Exception {
        this.getContentPane().setLayout(layoutMain);
        panelCenter.setLayout(null);
        this.setSize(new Dimension(400, 212));
        this.setTitle("Songs Download");
        statusBar.setText("");
        jButton1.setText("Start");
        jButton1.setEnabled(false);
        jButton1.setBounds(new Rectangle(240, 115, 65, 45));
        jButton1.setToolTipText("Start");
        jButton1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton1_actionPerformed(e);
                }
            });
        jLabel1.setText(jFile1.getCurrentDirectory().getAbsolutePath());
        jLabel1.setBounds(new Rectangle(5, 30, 370, 50));
        jButton2.setText("Pick Dir");
        jButton2.setBounds(new Rectangle(275, 85, 100, 20));
        jButton2.setDisabledIcon(imageOpen);
        jButton2.setIcon(imageOpen);
        jButton2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton2_actionPerformed(e);
                }
            });
        jButton3.setText("Stop");
        jButton3.setBounds(new Rectangle(310, 115, 65, 45));
        jButton3.setToolTipText("Stop");
        jButton3.setSize(new Dimension(65, 45));
        jButton3.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton3_actionPerformed(e);
                }
            });
        this.getContentPane().add(statusBar, BorderLayout.SOUTH);
        panelCenter.add(jButton3, null);
        panelCenter.add(jButton2, null);
        panelCenter.add(jLabel1, null);
        panelCenter.add(jButton1, null);
        this.getContentPane().add(panelCenter, BorderLayout.CENTER);
    }

    private void jButton1_actionPerformed(ActionEvent e) {
        mt = new MainThread(MOVIES_URL, jFile1.getSelectedFile());
        Thread t = new Thread(mt, "Main");
        t.start();
    }

    private void jButton2_actionPerformed(ActionEvent e) {
        jFile1.setDialogTitle("Pick base directory to store songs");
        jFile1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        jFile1.setAcceptAllFileFilterUsed(false);

        int dialog = jFile1.showDialog(this, "Select Dir");
        if (dialog == JFileChooser.APPROVE_OPTION) {
            File file = jFile1.getSelectedFile();
            jButton1.setEnabled(true);
            jLabel1.setText(file.getAbsolutePath());
        }
    }

    private void jButton3_actionPerformed(ActionEvent e) {
        if (mt != null) {
            mt.shutdownAndAwaitTermination();
        }
    }
}
